﻿using Chromium;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace M5.GeminiHybird.Core.XHR
{
    public class Routes
    {


        //public static void RouteConfig()
        //{
        //    MapHttpRoute(
        //        routeTemplate: "TeSt/AbC",
        //        controller: new Controller.TestController()
        //        );
        //}

        //private static Dictionary<string, string> MatchController { get; set; } = new Dictionary<string, string>();

        //internal static Dictionary<string, string> RawUrlAndCSharpMethodPairs { get; set; } = new Dictionary<string, string>();
        internal static Dictionary<string, Controller.BaseController> RawUrlAndControllerPairs { get; set; } = new Dictionary<string, Controller.BaseController>();
        //public static void MapHttpRoute(string routeTemplate, Controller.BaseController controller)
        //{

        //    if (RouteController.ContainsKey(routeTemplate))
        //    {
        //        throw new Exception($"已经存在{routeTemplate}的路由");
        //    }

        //    RouteController.Add(routeTemplate.ToLower(), controller);
        //}

        public static string RouteData(HttpListenerRequest request)
        {
            var httpMethod = request.HttpMethod.ToLower();
            string routeData = "";
            if (httpMethod == "get")
            {
                var rawUrlArray = request.RawUrl.Split('?');
                routeData = rawUrlArray[rawUrlArray.Length - 1];
            }
            else if (httpMethod == "post")
            {
                Stream stream = request.InputStream;
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                try
                {
                    routeData = reader.ReadToEnd();
                }
                catch
                {

                }
                
            }

            return routeData;
        }

        public static string RouteData(CfxRequest request, string rawUrl)
        {
            var httpMethod = request.Method.ToLower();
            string routeData = "";
            if (httpMethod == "get")
            {
                var rawUrlArray = rawUrl.Split('?');
                routeData = rawUrlArray[rawUrlArray.Length - 1];
            }
            else if (httpMethod == "post")
            {
                if (request.PostData != null)
                {
                    //foreach (var item in data.Elements)
                    //{
                    var postDataElement = request.PostData.Elements[0];
                    var size = postDataElement.GetBytes(postDataElement.BytesCount, postDataElement.NativePtr);
                    byte[] buffer = new byte[size];
                    Marshal.Copy(postDataElement.NativePtr, buffer, 0, (int)size);
                    routeData = Encoding.UTF8.GetString(buffer);

                    //}
                }
                //routeData = Encoding.UTF8.GetString(request.PostData.GetElements()[0].GetBytes());
            }

            return routeData;
        }

        public static Controller.BaseController FindController(string rawUrl)
        {
            rawUrl = rawUrl.ToLower();

            var array = rawUrl.Split('?');

            rawUrl = array[0];

            if (rawUrl.IndexOf('/') == 0)
            {
                rawUrl = rawUrl.Remove(0, 1);
            }

            if (!Routes.RawUrlAndControllerPairs.ContainsKey(rawUrl))
            {
                return null;
            }

            //string controllerClassPath = Routes.RawUrlAndCSharpMethodPairs[rawUrl];
            //controllerClassPath = controllerClassPath.Remove(controllerClassPath.LastIndexOf("."));
            Controller.BaseController baseController = Activator.CreateInstance(Routes.RawUrlAndControllerPairs[rawUrl].GetType()) as Controller.BaseController;

            
            var rawArray = rawUrl.Split('/');

            baseController.ActionName = rawArray[rawArray.Length - 1];
            return baseController;



            
        }
    }
}
